;
(function () {
  /**
   * Created by shen'jia.
   */
  'use strict';
  angular.module('services.omdisplay.oldHealthy', [])
    .service('oldHealthyData', oldHealthyDataServer) //页面依赖数据
    .service('chartOption', chartOptionServer) //echarts
    // .factory('processingData',processingDataServer)//响应时间处理
    .factory('oldHealthyServer', oldHealthyServer) //请求Url
    .factory('processingData', processingDataServer);
//数据
  function oldHealthyDataServer(COREDATA) {
    var provinceMy = COREDATA.PROVINCES;
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var day = now.getDate();
    var hour = now.getHours();
    this.selectLabel = '全部(汇总)';
    this.selectData = {
      business: [ {
        businessCode: '',
        businessName: '全部'
      }, {
        businessCode: '02',
        businessName: '充值'
      }],
      timeType: [{
        timeTypeName: '小时',
        timeTypeCode: '0'
      }, {
        timeTypeName: '天',
        timeTypeCode: '1'
      }],
      startTime: new Date(year, month, day - 6, hour),
      endTime: new Date(year, month, day, hour)
    };
    this.currentSendData = {
      businessLine: '', //业务线
      provinceCode: [], //省份
      tradeTypeCode: [], //交易类型
      timeTypeCode: '1',
      startTime: new Date(year, month, day - 6, hour),
      endTime: new Date(year, month, day, hour),
      systemCode: '1',
    };
    this.historySendData = {
      systemName: '统一支付',
      channelName: '全部',
      provinceName: '全部',
      businessName: '全部',
      timeTypeCode: '1',
      startTime: '',
      endTime: ''
    };
  }
//echarts图表的option
  function chartOptionServer() {
    this.currentOp = {
      title: {
        text: '',
        x: 'center',
        y: 30
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      legend: {
        data: ['天猫', '银行总对总', '移动商城']
      },
      animation: false,
      grid: {
        width: 980,
        x2: 120,
        x: 100
      },
      xAxis: [{
        min:95,
        max:100,
        type: 'value',
        boundaryGap: [0, 0.01],
        axisLabel: {
          formatter: '{value}%'
        },
        scale:true,
      }],
      yAxis: [{
        type: 'category',
        data: ['巴西'],
      },
      ],
      series: [{
        name: '银行总对总',
        type: 'bar',
        data: [90]
      }, {
        name: '天猫',
        type: 'bar',
        data: [90]
      }, {
        name: '移动商城',
        type: 'bar',
        data: [90]
      }]
    };
    this.currentBusinessOp = {
      title: {
        text: '',
        x: 'center',
        y: 30
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      legend: {
        data: ['缴费', '充值']
      },
      animation: false,
      grid: {
        width: 980,
        x2: 120,
        x: 100
      },
      xAxis: [{
        type: 'value',
        boundaryGap: [0, 0.01],
        scale:true,
        min:95,
        max:100,
        axisLabel: {
          formatter: '{value}%'
        }
      }],
      yAxis: [{
        type: 'category',
        data: ['银行总对总']
      }],
      series: [{
        name: '缴费',
        type: 'bar',
        data: [90]
      }, {
        name: '充值',
        type: 'bar',
        data: [90]
      }]
    };
    this.historyOp = {
      title: {
        text: '',
        x: 'center',
        y: 30
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      animation: false,
      legend: {
        data: ['天猫', '银行总对总', '移动商城']
      },
      grid: {
        width: 980,
        x2: 120,
        x: 100
      },
      xAxis: [{
        type: 'category',
        data: ['巴西'],
        axisLabel: {
          show: true,
          rotate: 50,
        },
      }],
      yAxis: [{
        type: 'value',
        axisLabel: {
          formatter: '{value}%'
        },
        boundaryGap: [0, 0.01],
        scale:true,
        min:95,
        max:100
      }],
      series: []
    };
    this.historyBusinessOp = {
      title: {
        text: '',
        x: 'center',
        y: 30
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      legend: {
        data: ['天猫', '银行总对总']
      },
      animation: false,
      grid: {
        width: 980,
        x2: 120,
        x: 100
      },
      xAxis: [{
        type: 'category',
        data: ['巴西'],
        rotate: 70,
        axisLabel:{
          show:true,
          rotate:50
        }
      }],
      yAxis: [{
        type: 'value',
        axisLabel: {
          formatter: '{value}%'
        },
        boundaryGap: [0, 0.01],
        scale:true,
        min:95,
        max:100
      }],
      series: [{
        name: '天猫',
        type: 'bar',
        data: [90]
      }, {
        name: '银行总对总',
        type: 'bar',
        data: [90]
      }]
    };
  }
//处理数据格式配合图表显示
  function processingDataServer(oldHealthyData) {
    return {
      process: processingCurr,
      processBU: processBU,
      processHistory: processHistory,
      processHistoryBU: processHistoryBU
    };

    function processingCurr(echartsObj, option, title, data,needTitle,titleName) {
      echartsObj.clear();
      option.series = [];
      for(var k=0;k<data[0].data1.length;k++){
        option.series[k] = {
          name: data[0].data1[k].channelName,
          type: 'bar',
          data: [90]
        };
      }
      // if (data[0].data1.length === 1) {
      //   option.series = [{
      //     name: '总对总',
      //     type: 'bar',
      //     data: [90]
      //   }];
      // } else {
      //   option.series = [{
      //     name: '总对总',
      //     type: 'bar',
      //     data: [90]
      //   }, {
      //     name: '天猫',
      //     type: 'bar',
      //     data: [90]
      //   }, {
      //     name: '移动商城',
      //     type: 'bar',
      //     data: [90]
      //   }];
      // }
      if(needTitle){
        option.title.text = data.length === 1 ? titleName+'当前健康度' : titleName+'不同省份当前健康度';
      }else{
        option.title.text ='';
      }
      echartsObj.setOption(option);
      var y = [];
      var listX = [];
      for (var ii = 0; ii < oldHealthyData.selectData.channel.length - 1; ii++) {
        listX[ii] = [];
      }

      for (var i = 0; i < data.length; i++) {
        y.push(data[i].provinceName);
        for (var j = 0; j < data[i].data1.length; j++) {
          listX[j].push(data[i].data1[j].channelHealth * 100);
        }
        if (data.length === 1) {
          y = ['全部省']
        };
        if (data.length === 1 && data[0].provinceName) {
          y = [data[0].provinceName]
        };
      }
      echartsObj.setOption(option);
      echartsObj.setOption({
        yAxis: [{
          data: y
        }]
      });
      var arraa = []
      for (var i = 0; i < listX.length; i++) {
        arraa[i] = {
          data: listX[i]
        };
      }
      echartsObj.setOption({
        series: arraa
      });

    }

    function processBU(echartsObj, option, title, data) {
      option.title.text = data.length === 1 ? '统一支付天猫,移动商城,总对总充值缴费当前健康度' : '统一支付天猫,移动商城,总对总充值缴费不同省份当前健康度';
      echartsObj.clear();
      echartsObj.setOption(option);
      var y = [];
      var listX = [];
      for (var ii = 0; ii < oldHealthyData.selectData.business.length; ii++) {
        listX[ii] = [];
      }

      for (var i = 0; i < data.length; i++) {
        y.push(data[i].provinceName);
        for (var j = 0; j < data[i].data1.length; j++) {
          listX[j].push(data[i].data1[j].businessHealth * 100);
        }
        if (data.length === 1 && data[0].provinceName===undefined) {
          y = ['全部省'];
        }
      }
      echartsObj.setOption(option);
      echartsObj.setOption({
        yAxis: [{
          data: y
        }]
      });
      var arraa = [];
      for (var i = 0; i < listX.length; i++) {
        arraa[i] = {
          data: listX[i]
        };
      }
      echartsObj.setOption({
        series: arraa
      });
    }

    function processHistory(echartsObj, option, title, data) {
      option.title.text = title;
      echartsObj.clear();
      echartsObj.setOption(option);
      var x = [];
      var y = [];
      var listY = [];
      for (var ii = 0; ii < oldHealthyData.selectData.channel.length - 1; ii++) {
        listY[ii] = [];
      }
      for (var i = 0; i < data.length; i++) {
        if (data[i].provinceName) {
          x.push(data[i].provinceName);
        } else {
          x.push(data[i].time);
        }

        for (var j = 0; j < data[i].data1.length; j++) {
          listY[j].push(data[i].data1[j].channelHealth * 100);
        }
        /*  if (data.length === 1) {
         y = ['全部省'];
         }*/
      }
      var arraa = [];
      option.series = [];
      for (var i = 0; i < data[0].data1.length; i++) {
        option.series[i] = {
          name: '1',
          type: 'bar',
          data: [10]
        };
        option.series[i].name = data[0].data1[i].channelName;
        arraa[i] = {
          data: listY[i]
        };
      }
      echartsObj.clear();
      echartsObj.setOption(option);
      echartsObj.setOption({
        xAxis: [{
          data: x
        }]
      });
      echartsObj.setOption({
        series: arraa
      });
    }

    function processHistoryBU(echartsObj, option, title, data) {
      option.title.text = title;
      echartsObj.clear();
      echartsObj.setOption(option);
      var x = [];
      var listY = [];
      for (var ii = 0; ii < oldHealthyData.selectData.business.length; ii++) {
        listY[ii] = [];
      }

      for (var i = 0; i < data.length; i++) {
        if (data[i].provinceName) {
          x.push(data[i].provinceName);
        } else {
          x.push(data[i].time);
        }
        for (var j = 0; j < data[i].data1.length; j++) {
          listY[j].push(data[i].data1[j].businessHealth * 100);
        }
        /*  if (data.length === 1) {
         x = ['全部省'];
         }*/
      }
      echartsObj.setOption(option);
      echartsObj.setOption({
        xAxis: [{
          data: x
        }]
      });
      var arraa = [];
      for (var i = 0; i < listY.length; i++) {
        arraa[i] = {
          data: listY[i]
        };
      }
      echartsObj.setOption({
        series: arraa
      });
    }
  }
//URL请求
  function oldHealthyServer(Restangular, oldHealthyData, CoreService, processingData) {
    function quanbu(obj, name) {
      var strings = '';
      for (var i = 1; i < obj.length; i++) {
        strings += obj[i][name] + ',';
      }
      strings = strings.substr(0, strings.length - 1);
      return strings;
    }

    function currentParams() {
      var params = oldHealthyData.currentSendData;
      console.log(params);
      params = angular.copy(oldHealthyData.currentSendData);
      if (params.channelName === '全部') {
        params.channelName = quanbu(oldHealthyData.selectData.channel, 'channelName');
      }
      if (params.province && params.province.length > 0) {
        var proName = '';
        for (var i = 0; i < params.province.length; i++) {
          proName += params.province[i].provinceName + ',';
        }
        params.provinceName = proName.substr(0, proName.length - 1);
        params.province = undefined;
      } else if (oldHealthyData.selectLabel === '全部(汇总)') {
        params.provinceName = '不分省';
        params.province = undefined;
      } else if (oldHealthyData.selectLabel === '全部(分省)') {
        params.provinceName = '全部';
        params.province = undefined;
      }
      return params;
    }

    function historyParams() {
      var params = {};
      params = angular.copy(oldHealthyData.historySendData);
      if (params.timeTypeCode === '1') {
        var stime = CoreService.dateFormat(oldHealthyData.selectData.startTime, 'yyyy-MM-dd');
        var etime = CoreService.dateFormat(oldHealthyData.selectData.endTime, 'yyyy-MM-dd');
      } else {
        var stime = CoreService.dateFormat(oldHealthyData.selectData.startTime, 'yyyy-MM-dd hh');
        var etime = CoreService.dateFormat(oldHealthyData.selectData.endTime, 'yyyy-MM-dd hh');
      }
      params.startTime = stime;
      params.endTime = etime;
      if (params.channelName === '全部') {
        params.channelName = quanbu(oldHealthyData.selectData.channel, 'channelName');
      }
      if (params.province && params.province.length > 0) {
        var proName = '';
        for (var i = 0; i < params.province.length; i++) {
          proName += params.province[i].provinceName + ',';
        }
        params.provinceName = proName.substr(0, proName.length - 1);
        params.province = undefined;
      } else if (oldHealthyData.selectLabel === '全部(汇总)') {
        params.provinceName = '不分省';
        params.province = undefined;
      } else if (oldHealthyData.selectLabel === '全部(分省)') {
        params.provinceName = '全部';
        params.province = undefined;
      }
      return params;
    }
    return {
      //current响应时间
      current: function(params,callback) {
        Restangular.all('sas/OMdisplay/oldHealthy/history').post(params).then(callback);
      },
      //current响应时间（下钻）
      currentBusiness: function(chart, option, title) {
        var params = currentParams();
        params.channelName = undefined;
        params.businessName = '缴费,充值';
        Restangular.all('sas/OMdisplay/oldHealthy/current/business').post(params).then(function(res) {
          if (res && res.state === '10000') {
            processingData.processBU(chart, option, title, res.data);
          }
        });
      },
      //history响应时间
      history: function(chart, option, title) {
        var param = historyParams();
        title = param.provinceName === '不分省' ? param.startTime + '到' + param.endTime + '天猫移动商城,总对总健康度' : param.startTime + '到' + param.endTime + '天猫移动商城,总对总不同地区健康度';
        Restangular.all('sas/OMdisplay/oldHealthy/history').post(param).then(function(res) {
          if (res && res.state === '10000') {
            processingData.processHistory(chart, option, title, res.data);
          }
        });
      },
      //history响应时间（下钻）
      historyBusiness: function(chart, option, title) {
        var params = historyParams();
        params.channelName = undefined;
        params.businessName = '缴费,充值';
        title = params.provinceName === '不分省' ? params.startTime + '到' + params.endTime + '天猫移动商城,总对总充值缴费健康度' : params.startTime + '到' + params.endTime + '天猫移动商城,总对总不同地区充值缴费健康度';
        Restangular.all('sas/OMdisplay/oldHealthy/history/business').post(params).then(function(res) {
          if (res && res.state === '10000') {
            processingData.processHistoryBU(chart, option, title, res.data);
          }
        });
      },
      queryTradeType: function(params,callback) {
        Restangular.all('sas/common/tradeTypeQuery').post(params).then(callback);
      },
    }
  }
})();